package cn.lingyangwl.agile.common.datasource.core.handler;

import cn.lingyangwl.framework.security.LySecurityAutoConfiguration;
import cn.lingyangwl.agile.common.security.utils.SecurityUtils;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Component;

/**
 * mybatis-plus的高级操作
 * @author shenguangyang
 */
@ConditionalOnClass(LySecurityAutoConfiguration.class)
@Component
public class OperationUserFillHandler implements EntityFieldFillHandler {
    private static final Logger log = LoggerFactory.getLogger(OperationUserFillHandler.class);
    @Override
    public void insertFill(MetaObject metaObject, MetaObjectHandler metaObjectHandler) {
        if (metaObject.hasSetter("createBy")) {
            metaObject.setValue("createBy",null);
            metaObjectHandler.strictInsertFill(metaObject, "createBy", String.class, SecurityUtils.getUserName());
        }

        if (metaObject.hasSetter("createdBy")) {
            metaObject.setValue("createdBy",null);
            metaObjectHandler.strictInsertFill(metaObject, "createdBy", String.class, SecurityUtils.getUserName());
        }

        if (metaObject.hasSetter("createdUid")) {
            metaObject.setValue("createdUid",null);
            metaObjectHandler.strictInsertFill(metaObject, "createdUid", Long.class, SecurityUtils.getUserId());
        }

    }

    @Override
    public void updateFill(MetaObject metaObject, MetaObjectHandler metaObjectHandler) {
        if (metaObject.hasSetter("updateBy")) {
            metaObjectHandler.strictUpdateFill(metaObject, "updateBy", String.class, SecurityUtils.getUserName());
        }

        if (metaObject.hasSetter("updatedBy")) {
            metaObjectHandler.strictUpdateFill(metaObject, "updatedBy", String.class, SecurityUtils.getUserName());
        }
    }
}
